package com.xtravel.discovery.api;

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import com.xtravel.common.api.BaseRequest;
import com.xtravel.common.api.domain.BaseJson;
import com.xtravel.discovery.dto.CountryState;
import com.xtravel.discovery.dto.LiveRecord;
import com.xtravel.discovery.dto.StateLiveRecord;
import com.xtravel.discovery.dto.TalentDto;

/**
 * Created by Memory on 2015/8/3.
 */
public class LiveRequest extends BaseRequest {

    public List<LiveRecord> getAllLiveRecords(Long sinceId, Long maxId, int count) {
        try {
            String response = api(String.format("live/list/%d/%d/%d/0", sinceId, maxId, count));
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            LiveRecordResult json = gson.fromJson(response, LiveRecordResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public List<LiveRecord> getCountryLiveRecords(Long sinceId, Long maxId, int count, String countryId) {
        try {
            String response = api(String.format("live/list/%d/%d/%d/%s", sinceId, maxId, count, countryId));
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            LiveRecordResult json = gson.fromJson(response, LiveRecordResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    //正在直播的列表
    public List<LiveRecord> getLiveRecords(Long sinceId, Long maxId, int count) {
        try {
            String response = api(String.format("live/list/%d/%d/%d", sinceId, maxId, count));
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            LiveRecordResult json = gson.fromJson(response, LiveRecordResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public List<StateLiveRecord> getStateLiveRecords(Long sinceId, Long maxId, int count, String stateId) {
        try {
            //livest/{pageIndex}/{count}/{state}
            String response = api(String.format("live/states/%d/%d/%d/%s", sinceId, maxId, count, stateId));
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            StateLiveRecordResult json = gson.fromJson(response, StateLiveRecordResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public List<CountryState> getCountryStates() {
        try {
            String response = api("live/state");
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            CountryStateResult json = gson.fromJson(response, CountryStateResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    //获取直播相关的达人
    public List<TalentDto> getRelativeTalents(int liveId){
        try {
            String response = api("tourism/" + liveId);
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            TalentsResult json = gson.fromJson(response, TalentsResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    //获取达人圈达人列表
    public List<TalentDto> getTalentList(Long sinceId, Long maxId, int count){
        try {
            String response = api(String.format("tourism/list/%d/%d/%d", sinceId, maxId, count));
            if (response == null)
                return new ArrayList<>();

            Gson gson = new Gson();
            TalentsResult json = gson.fromJson(response, TalentsResult.class);
            if (json.getCode() == 200)
                return json.getResult();

            return new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    public boolean like(Long liveId, boolean isOk) {
        String response = post("live/like/" + liveId.toString(), String.valueOf(isOk));
        if (response == null)
            return false;

        try {
            Gson gson = new Gson();
            BaseJson<String> json = gson.fromJson(response, LiveActionResult.class);
            return (json != null && json.getCode() == 200);
        } catch (Exception e) {
            return false;
        }
    }

    public boolean favorite(Long liveId, boolean isOk) {
        String response = post("live/favorite/" + liveId, String.valueOf(isOk));
        if (response == null)
            return false;

        try {
            Gson gson = new Gson();
            BaseJson<String> json = gson.fromJson(response, LiveActionResult.class);
            return (json != null && json.getCode() == 200);
        } catch (Exception e) {
            return false;
        }
    }

    public boolean share(Long liveId) {
        String response = post("live/share", liveId.toString());
        if (response == null)
            return false;

        try {
            Gson gson = new Gson();
            BaseJson<String> json = gson.fromJson(response, LiveActionResult.class);
            return (json != null && json.getCode() == 200);
        } catch (Exception e) {
            return false;
        }
    }
}
